package cn.otrue.patienthealthmanager.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.activity.DactorSouSuoActivity;
import cn.otrue.patienthealthmanager.adapter.MyFragmentPagerAdpater;
import cn.otrue.patienthealthmanager.adapter.ScrollTabsAdapter;
import cn.otrue.patienthealthmanager.view.ScrollTabView;

/**
 * 找医生
 * 
 * @author G
 * 
 */
public class FindDoctorFragment extends BaseFragment {

	private static ViewPager vp_doctor_viewpager;
	private ScrollTabView st_doctor_tabs;
	private ScrollTabsAdapter tabsAdapter;
	private MyFragmentPagerAdpater adapter;
	private TextView tv_title_other;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_doctor, container,
				false);
		initView(rootView);
		vp_doctor_viewpager.setCurrentItem(0);
		return rootView;
	}

	private void initView(View rootView) {

		st_doctor_tabs = (ScrollTabView) rootView
				.findViewById(R.id.st_doctor_tabs);
		vp_doctor_viewpager = (ViewPager) rootView
				.findViewById(R.id.vp_doctor_viewpager);
		tv_title_other = (TextView) getActivity().findViewById(
				R.id.tv_title_oth);

		tv_title_other.setOnClickListener(this);

		initViewPagerList();

	}

	private void initViewPagerList() {

		adapter = new MyFragmentPagerAdpater(getFragmentManager());
		adapter.addFragment(new FindDoctorAllFragment());
		adapter.addFragment(new FindDoctorAttentionFragment());
		adapter.addFragment(new FindDoctorPrivateFragment());
		adapter.addFragment(new FindDoctorChatLogFragment());

		vp_doctor_viewpager.setAdapter(adapter);

		tabsAdapter = new ScrollTabsAdapter(getActivity(), 4);
		tabsAdapter.add(getString(R.string.all_doctor));
		tabsAdapter.add(getString(R.string.attention_doctor));
		tabsAdapter.add(getString(R.string.private_doctor));
		tabsAdapter.add(getString(R.string.chat_log));

		st_doctor_tabs.setAdapter(tabsAdapter);
		st_doctor_tabs.setViewPager(vp_doctor_viewpager);
		vp_doctor_viewpager.setOffscreenPageLimit(3);
	}

	@Override
	public void onClick(View arg0) {

	}



	public static Fragment newInstance() {
		return new FindDoctorFragment();
	}

	@Override
	public String setPageName() {
		// TODO Auto-generated method stub
		return null;
	}

	public static void gotoFragment(int i) {
		vp_doctor_viewpager.setCurrentItem(i);
	}

	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);

		if (isVisibleToUser) {

			if (tv_title_other != null) {

				tv_title_other.setVisibility(View.VISIBLE);
				tv_title_other.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Intent intent = new Intent();
						intent.setClass(getActivity(),
								DactorSouSuoActivity.class);
						startActivity(intent);

					}
				});
			}
		} else {
			// 相当于Fragment的onPause
			if (tv_title_other != null) {
				tv_title_other.setVisibility(View.GONE);
			}
		}
	}

}
